# Required vars with defaults
# - openqa_hostname
##      string - hostname of openQA server to run jobs for
##      default - localhost
# - openqa_consumer
##      bool - whether to enable the fedmsg consumer and run whenever
##             a compose completes
##      default - true
# - external_hostname
##       string - The public hostname of the openQA server (used in ResultsDB links)
##       default - ansible_nodename
#
# Optional vars
# - wikitcms_user
##      string - FAS username for reporting results to wiki
# - wikitcms_password
##      string - password for relval_user
# - deployment_type
##      string - Fedora Infrastructure thing; for this role, the
##               fedora_openqa config file will be set appropriately
##               for infra deployments if this is set, so don't set
##               it for private deployments
#
# When all of the above are set, a wikitcms 'credentials' file will
# be created and result submission to the wiki will be enabled. If
# deployment_type is set to 'prod', results will be submitted to the
# production wiki in response to openQA production 'job complete'
# fedmsgs; if set to 'stg', results will be submitted to the staging
# wiki in response to openQA staging 'job complete' fedmsgs. Result
# reporting to ResultsDB will also be enabled for the production
# deployment. You probably should NOT set these unless you're
# maintaining the Fedora infrastructure deployments.
#
# NOTE: There are still currently a couple of assumptions that the
# openQA server boxes will always act as their own dispatchers, but
# there is no longer any fundamental reason why this must be the case,
# as we now use the openQA asset downloading capability so that the
# scheduler is no longer responsible for downloading assets.

# just keeping this around for convenience as we often need it
#- name: Install required packages (testing)
#  dnf: name={{ item }} state=present enablerepo="updates-testing"
#  with_items:

#  tags:
#  - packages

- name: Install required packages
  dnf: name={{ item }} state=present
  with_items:
  - python2-fedfind
  - python2-fedmsg-consumers
  - python2-openqa_client
  - python2-requests
  - python-resultsdb_api
  - python2-resultsdb_conventions-fedora
  - python-setuptools
  - python-six
  - python2-wikitcms
  tags:
  - packages

- name: Check tools directory exists with correct ownership
  file: path=/root/fedora_openqa state=directory owner=root group=root

# we don't want to run the checkout if the tests are on a non-standard
# branch, as that usually means we're messing around on staging and
# don't want the checkout reset to HEAD.
- name: Check if tests are checked out and on a non-standard branch
  command: "git status"
  args:
    chdir: /root/fedora_openqa
  register: toolsbranch
  failed_when: "1 != 1"
  changed_when: "1 != 1"
  check_mode: no

- name: Check out fedora_openqa (scheduler / reporter tool)
  git:
    repo: https://pagure.io/fedora-qa/fedora_openqa.git
    dest: /root/fedora_openqa
  register: gittools
  when: "(toolsbranch.stderr.find('Not a git repository') != -1) or (toolsbranch.stdout.find('On branch master') != -1)"

- name: Check if fedora_openqa has ever been installed
  stat: path=/usr/bin/fedora-openqa
  register: insttools
  changed_when: "1 != 1"
  failed_when: "1 != 1"
  check_mode: no

- name: Install fedora_openqa
  command: "python setup.py install"
  args:
    chdir: /root/fedora_openqa
  when: "gittools|changed or not insttools.stat.exists"
  notify:
  - restart fedmsg-hub

# For now, we want ppc64 jobs generated on staging but not prod (as we
# have ppc64 workers for stg but not prod). So we have a WANTED file
# for staging.
- name: Install ppc64-imbued WANTED file if this is staging
  copy: src=images.json.ppc64 dest=/etc/fedora-openqa/images.json
  when: (deployment_type is defined) and (deployment_type == 'stg')
  notify:
  - restart fedmsg-hub

- name: openQA client config
  template: src=client.conf.j2 dest=/etc/openqa/client.conf owner=root group=fedmsg mode=0640
  tags:
  - config

- name: Create fedora_openqa config directory
  file: path=/etc/fedora-openqa state=directory owner=root group=root mode=0755

- name: Write schedule.conf
  template: src=schedule.conf.j2 dest=/etc/fedora-openqa/schedule.conf owner=root group=root mode=0644
  notify:
  - restart fedmsg-hub
  tags:
  - config

- name: Create /etc/fedora (credentials files location)
  file: path=/etc/fedora state=directory owner=root group=fedmsg mode=0750

- name: Write wikitcms credentials file
  template: src=credentials.j2 dest=/etc/fedora/credentials owner=root group=fedmsg mode=0640
  when: "wikitcms_user is defined and wikitcms_password is defined"
  tags:
  - config

- name: Enable fedmsg consumer
  template: src=openqa_consumer.py.j2 dest=/etc/fedmsg.d/openqa_consumer.py owner=root group=root mode=0644
  notify:
  - restart fedmsg-hub
  tags:
  - config

- name: Set up cron job to schedule live-respins jobs
  copy: src=schedule-live-respins.cron dest=/etc/cron.hourly/schedule-live-respins owner=root group=root mode=0755
  tags:
  - config
